﻿using System;
using System.Configuration;
using System.Web.Configuration;

namespace YBBYunQi
{
    /// <summary>
    /// 提供对.config文件的访问
    /// </summary>
    public sealed class ConfigUtil
    {
        /// <summary>
        /// 获取配置字符串(&lt;add key='' value='' /&gt;)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfig(string key)
        {
            if (ConfigurationManager.AppSettings[key] != null)
            {
                return ConfigurationManager.AppSettings[key].ToString();
            }
            else
                return null;
        }

        /// <summary>
        /// 获取数据库连接字符串(&lt;connectionStrings&gt;节点)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConnString(string key)
        {
            return ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }

        /// <summary>
        /// 设置/重写一个key:value对
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetSetting(string key, string value)
        {
            try
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
                AppSettingsSection section = config.AppSettings;
                if (section.Settings[key] == null)
                {
                    section.Settings.Add(key, value);
                }
                else
                {
                    section.Settings[key].Value = value;
                }
                config.Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 设置/重写一个数据库连接串
        /// </summary>
        /// <param name="key"></param>
        /// <param name="connString"></param>
        /// <returns></returns>
        public static bool SetConnString(string key, string connString)
        {
            try
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
                ConnectionStringsSection section = config.ConnectionStrings;
                if (section.ConnectionStrings[key] == null)
                {
                    section.ConnectionStrings.Add(new ConnectionStringSettings(key, connString));
                }
                else
                {
                    section.ConnectionStrings[key].ConnectionString = connString;
                }
                config.Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个key:value节点
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool RemoveSetting(string key)
        {
            try
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
                AppSettingsSection section = config.AppSettings;
                if (section.Settings[key] != null)
                {
                    section.Settings.Remove(key);
                }
                config.Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取指定的Bool值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetConfigBool(string key)
        {
            bool flag = false;
            string configString = GetConfigString(key);
            if ((configString != null) && (string.Empty != configString))
            {
                try
                {
                    flag = bool.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return flag;
        }

        /// <summary>
        /// 获取指定的Decimal值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static decimal GetConfigDecimal(string key)
        {
            decimal num = 0M;
            string configString = GetConfigString(key);
            if ((configString != null) && (string.Empty != configString))
            {
                try
                {
                    num = decimal.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return num;
        }

        /// <summary>
        /// 获取指定的Int值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetConfigInt(string key)
        {
            int num = 0;
            string configString = GetConfigString(key);
            if ((configString != null) && (string.Empty != configString))
            {
                try
                {
                    num = int.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return num;
        }

        /// <summary>
        ///  获取指定的String值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigString(string key)
        {
            string cacheKey = key;
            object cache = SiteCache.Get(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = ConfigurationManager.AppSettings[key];
                    if (cache != null)
                    {
                         SiteCache.Max(cacheKey, cache);
                    }
                    else
                    {
                        return string.Empty;
                    }
                }
                catch
                {
                    return string.Empty;
                }
            }
            return cache.ToString();
        }

    }
}
